//
//  MessageModel.m
//  糗百——王桂敏
//
//  Created by 王桂敏 on 15/5/25.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "MessageModel.h"
#define kSCREENWIDTH [UIScreen mainScreen].bounds.size.width

@implementation MessageModel
//根据文本内容来确定文本的高度
- (CGRect)contentRect{
    CGRect targetRect = [self.content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 2000) options:NSStringDrawingUsesFontLeading |NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:13]} context:nil];
    return targetRect;
}
//最终的行高
- (CGFloat)rowHeight{
//    NSLog(@"%f",self.contentRect.size.height);
    CGFloat height = self.contentRect.size.height + 48 + 5 + 28 + 98 / 3;
    if (self.image) {
        height += [self.image_size[@"s"][1] floatValue] / [[self.image_size[@"s"] firstObject] floatValue] * (kSCREENWIDTH - 20) + 5;

    }
    if (self.pic_url) {
        height += 5 + kSCREENWIDTH - 20;
    }
    return height;
}
//初始化方法
- (id)initWithDictionary:(NSDictionary *)dictionary{
    self = [super init];
    if (self) {
        self.message_id = dictionary[@"id"];
        self.content = dictionary[@"content"];
        self.comments_count = dictionary[@"comments_count"];
//        self.published_at = [dictionary[@"published_at"] stringValue];
        if (![dictionary[@"user"] isKindOfClass:[NSNull class]]) {//判断user键值对是否存在，不存在就不能传值
            self.user = [[[UserModel alloc] initWithDictionary:dictionary[@"user"]] autorelease];
        }

        self.votes = dictionary[@"votes"];
        self.detail_url = [NSString stringWithFormat:@"http://m2.qiushibaike.com/article/%@/comments?count=50&page=1&AdID=1432516254311507B38D95",self.message_id];
        //判断dictionary中知否存在图片和视频
        if (![dictionary[@"image"] isKindOfClass:[NSNull class]]) {//判断字典中是否存在该键值对
            self.image = dictionary[@"image"];
            self.image_size = dictionary[@"image_size"];
        } else {
            self.image = nil;
            self.image_size = @{@"m":@[@0,@0,@0],@"s":@[@0,@0,@0]};
        }
        if (dictionary[@"low_url"]) {
            self.low_url = dictionary[@"low_url"];
            self.pic_size = dictionary[@"pic_size"];
            self.pic_url = dictionary[@"pic_url"];
        } else {
            self.pic_size = @[@0,@0];
            self.low_url = nil;
            self.pic_url = nil;
        }

    }
    return self;
}
//编码协议是让当前类的对象通过固定的编码规则转成NSdata类型的数据
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.message_id forKey:@"message_id"];
    [aCoder encodeObject:self.content forKey:@"content"];
    [aCoder encodeObject:self.comments_count forKey:@"comments_count"];
    [aCoder encodeObject:self.user forKey:@"user"];
    [aCoder encodeObject:self.votes forKey:@"votes"];
    [aCoder encodeObject:self.detail_url forKey:@"detail_url"];
    [aCoder encodeObject:self.image_size forKey:@"image_size"];
    [aCoder encodeObject:self.image forKey:@"image"];
    [aCoder encodeObject:self.low_url forKey:@"low_url"];
    [aCoder encodeObject:self.pic_size forKey:@"pic_size"];
    [aCoder encodeObject:self.pic_url forKey:@"pic_url"];
}
//解码协议，是在反归档的时候将NSData类型的数据转换成当前类的对象时调用的。解码时用到的key要跟编码时指定的key保持一致
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.message_id = [aDecoder decodeObjectForKey:@"message_id"];
        self.content = [aDecoder decodeObjectForKey:@"content"];
        self.comments_count = [aDecoder decodeObjectForKey:@"comments_count"];
        self.user = [aDecoder decodeObjectForKey:@"user"];
        self.votes = [aDecoder decodeObjectForKey:@"votes"];
        self.detail_url = [aDecoder decodeObjectForKey:@"detail_url"];
        self.image = [aDecoder decodeObjectForKey:@"image"];
        self.image_size = [aDecoder decodeObjectForKey:@"image_size"];
        self.low_url = [aDecoder decodeObjectForKey:@"low_url"];
        self.pic_url = [aDecoder decodeObjectForKey:@"pic_url"];
        self.pic_size = [aDecoder decodeObjectForKey:@"pic_size"];
    }
    return self;
}
- (void)dealloc{
    [_message_id release];
    [_content release];
    [_comments_count release];
//    [_published_at release];
    [_user release];
    [_votes release];
    [_detail_url release];
    [_image release];
    [_image_size release];
    [_low_url release];
    [_pic_size release];
    [_pic_url release];
    [super dealloc];
}


@end
